package com.wontube.smr.bo.sites.moviefone;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.wontube.client.extension.utils.ArrayUtils;
import com.wontube.client.extension.utils.StringUtils;
import com.wontube.smr.bo.constants.SiteConstants;
import com.wontube.smr.bo.constants.StandarMovieGenres;
import com.wontube.smr.client.model.po.Genre;
import com.wontube.smr.dao.bo.GenreSelectDao;
import com.wontube.smr.web.client.model.po.GenreSite;

/**
 * moviefone genre map
 * 
 * @author hewc@wondershare.cn
 * 
 */
public final class MovieFontMovieGenres {
	private MovieFontMovieGenres() {		
	}
	
	private static final Map<String, String> siteStandarMap = new HashMap<String, String>();		
	private static final Map<String, String> map = new HashMap<String, String>();		

	static {
		loadMovieGenersMap();
		
		//"action",Musicals, Art and Experimental, Crime and Mystery, Epic and Historical, Westerns, 
		//Science Fiction,  Thriller, Sport, Documentary & Biography, Animation & Cartoons, Arts and Culture
		//Action and Adventure, Special Interest, Live Performances
		map.put("action", StandarMovieGenres.ACTION);		
		map.put("Art and Experimental", StandarMovieGenres.CULTURE);		
		map.put("Crime and Mystery", StandarMovieGenres.CRIME);		
		map.put("Epic and Historical", StandarMovieGenres.HISTORY);		
		map.put("Westerns", StandarMovieGenres.OTHERS);		
		map.put("Science Fiction", StandarMovieGenres.SCIENCE);		
		map.put("Thriller", StandarMovieGenres.HORROR);		
		map.put("Sport", StandarMovieGenres.SPORTS);		
		map.put("Documentary & Biography", StandarMovieGenres.Documentary);		
		map.put("Animation & Cartoons", StandarMovieGenres.ANIMATION);		
		map.put("Arts and Culture", StandarMovieGenres.CULTURE);		
		map.put("Action and Adventure", StandarMovieGenres.ACTION);				
		map.put("Special Interest", StandarMovieGenres.OTHERS);
		map.put("Live Performances", StandarMovieGenres.OTHERS);		
		
		//TODO		
	}
	
	private static void loadMovieGenersMap(){
		List<GenreSite> genreSites = GenreSelectDao.selectGenreSites(SiteConstants.WEBSITE_MOVIEFONE, 
				Genre.CATE_MOVIE);
		for (GenreSite gs : genreSites) {
			siteStandarMap.put(gs.getSiteGenre(), gs.getStandarGenre());
		}
	}
	
	/**
	 * 修正单个genre
	 * @param mfGenre
	 * @return
	 */
	public static String remendGenre(String mfGenre){
		if(StringUtils.isNullOrEmpty(mfGenre)){
			return null;
		}
		
		//处理专有genre, 不存在则判断是标准genre
		mfGenre = mfGenre.trim();
		String genre = map.get(mfGenre);
		return genre != null ? genre : mfGenre;
	}
	
	/**
	 * 处理全部genres(逗号分隔), 并防止重复
	 * @param genreNames
	 * @return
	 */
	public static String remendAllGenres(String genreNames){
		if(StringUtils.isNullOrEmpty(genreNames)){
			return null;
		}
		
		Set<String> genreSet = new HashSet<String>();
		
		String[] genres = genreNames.split("\\s*,\\s*");
		for (String g : genres) {
			String rg = remendGenre(g);
			if(rg != null){
				genreSet.add(rg);
			}		
		}
		
		return ArrayUtils.join(genreSet, ",");
	}
}
